﻿Imports System.Windows.Forms
Imports DotSpatial.Controls
Imports DotSpatial.Data
Imports DotSpatial.Symbology
Imports DotSpatial.Topology

Public Class frmSelectAttribute
    Private lyrGrid As FeatureLayer = Nothing

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
        Dim filter As String = ""
        For i As Integer = 1 To 4
            Dim fld As String = Choose(i, "Elevation", "Roughness", "Vegetation", "Cell_Group")
            Dim cbo As ComboBox = Choose(i, cboElev, cboRough, cboVeg, cboGrp)
            Dim txt As String = Choose(i, txtElev, txtRough, txtVeg, txtGrp).text
            If cbo.SelectedIndex <> -1 And txt.Trim <> "" Then
                If filter <> "" Then filter &= " AND "
                If i < 4 Then 'numeric comparisons
                    Dim oper As String = Choose(cbo.SelectedIndex + 1, "=", "<>", "<", ">", "BETWEEN")
                    If oper = "BETWEEN" Then
                        Dim t() As String = txt.Split(",")
                        filter &= String.Format("{0} >= {1} AND {0} <= {2}", fld, t(0), t(1))
                    Else
                        filter &= String.Format("{0} {1} {2}", fld, oper, txt)
                    End If
                Else 'string comparisons
                    Dim oper As String = Choose(cbo.SelectedIndex + 1, "=", "<>", "IN", "LIKE")
                    If oper = "IN" Then
                        Dim t() As String = txt.Split(",")
                        txt = ""
                        For Each s As String In t
                            txt &= IIf(txt = "", "", ",") & "'" & s & "'"
                        Next
                        filter &= String.Format("{0} {1} ({2})", fld, oper, txt)
                    Else
                        filter &= String.Format("{0} {1} '{2}'", fld, oper, txt)
                    End If
                End If
            End If
        Next
        lyrGrid.UnSelectAll()
        If filter <> "" Then lyrGrid.SelectByAttribute(filter)
        GISForm.GISMap.Refresh()
        lblSelectedCount.Text = String.Format("{0} Cells Selected", lyrGrid.Selection.ToFeatureList.Count)
        btnEdit.Enabled = lyrGrid.Selection.ToFeatureList.Count > 0
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub txt_click(sender As Object, e As System.EventArgs) Handles txtElev.click, txtRough.click, txtVeg.click
        Dim txt As TextBox = Nothing
        Dim cbo As ComboBox = Nothing
        If sender Is txtElev Then
            txt = txtElev
            cbo = cboElev
        ElseIf sender Is txtRough Then
            txt = txtRough
            cbo = cboRough
        ElseIf sender Is txtVeg Then
            txt = txtVeg
            cbo = cboVeg
        End If
        If txt.Text = "" Then cbo.SelectedIndex = 0
    End Sub

    Private Sub txt_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles txtElev.Validating, txtRough.Validating, txtVeg.Validating
        Dim cbo As ComboBox = cboElev
        Dim txt As TextBox = sender
        If sender Is txtRough Then
            cbo = cboRough
        ElseIf sender Is txtVeg Then
            cbo = cboVeg
        End If
        If cbo.Text Like "Between" AndAlso txt.Text <> "" AndAlso txt.Text.Split(",").Length <> 2 Then
            WarningMsg("You must enter lower and upper limits separated by a comma.")
            e.Cancel = True
        End If
    End Sub

    Private Sub txt_GotFocus(sender As Object, e As System.EventArgs) Handles txtRough.GotFocus, txtVeg.GotFocus, txtElev.GotFocus
        With CType(sender, TextBox)
            .SelectAll()
        End With
    End Sub

    Private Sub txtGrp_Enter(sender As Object, e As System.EventArgs) Handles txtGrp.Enter
        txtGrp.SelectAll()
    End Sub

    Private Sub txtGrp_click(sender As Object, e As System.EventArgs) Handles txtGrp.click, txtGrp.SelectionChangeCommitted
        If (Not String.IsNullOrEmpty(txtGrp.Text) OrElse txtGrp.SelectedIndex <> -1) AndAlso cboGrp.SelectedIndex = -1 Then cboGrp.SelectedIndex = 0
    End Sub

    Private Sub LoadForm()
        For i As Integer = 1 To 3
            Dim cbo As ComboBox = Choose(i, cboElev, cboRough, cboVeg)
            cbo.Items.Clear()
            cbo.Items.AddRange(New String() {"Equal To", "Not Equal To", "Less Than", "Greater Than", "Between"})
            cbo.SelectedIndex = 0
        Next

        cboGrp.Items.Clear()
        cboGrp.Items.AddRange(New String() {"Equal To", "Not Equal To", "In", "Like"})
        cboGrp.SelectedIndex = 0

        Dim lstCellGroups As New List(Of String)
        For Each f As Feature In lyrGrid.DataSet.Features
            Dim grp As Object = f.DataRow("Cell_Group")
            If Not IsDBNull(grp) AndAlso Not String.IsNullOrEmpty(grp) AndAlso Not lstCellGroups.Contains(grp) Then
                lstCellGroups.Add(grp)
            End If
        Next
        txtGrp.Items.Clear()
        txtGrp.Items.AddRange(lstCellGroups.ToArray)
    End Sub

    Public Sub New(GridLayer As FeatureLayer)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        lyrGrid = GridLayer
    End Sub

    Private Sub frmSelectCells_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        LoadForm()
    End Sub

    Private Sub btnReset_Click(sender As System.Object, e As System.EventArgs) Handles btnReset.Click
        For i As Integer = 1 To 4
            Dim cbo As ComboBox = Choose(i, cboElev, cboRough, cboVeg, cboGrp)
            cbo.SelectedIndex = 0
            Dim txt As Object = Choose(i, txtElev, txtRough, txtVeg, txtGrp)
            txt.text = ""
        Next
        btnSelect.PerformClick()
    End Sub

End Class
